package com.fy.pojo.enums;

import com.fy.utils.DateFormatter;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public enum ExpireTimeEnum {
    ONE_DAY(0, 1),
    ONE_WEEK(1, 7),
    ONE_MONTH(2, 30),
    PERMANENT(3, -1); // -1 表示永久有效，不计算天数

    private final int type;
    private final int days;

    ExpireTimeEnum(int type, int days) {
        this.type = type;
        this.days = days;
    }

    public int getType() {
        return type;
    }

    public int getDays() {
        return days;
    }

    /**
     * 获取当前时间增加指定天数后的字符串表示
     *
     * @return 增加指定天数后的字符串表示，格式为 "yyyy-MM-dd HH:mm:ss"
     */
    public String getDateStringAfterDays() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime future;
        if (this.days == -1) {
            // 永久有效，假设为100年后
            future = now.plusYears(100);
        } else {
            future = now.plus(this.days, ChronoUnit.DAYS);
        }
        return DateFormatter.formatter.format(future);
    }

    // 创建一个静态映射表，用于根据类型查找枚举值
    private static final Map<Integer, ExpireTimeEnum> BY_TYPE =
            Stream.of(values()).collect(Collectors.toMap(ExpireTimeEnum::getType, e -> e));

    /**
     * 根据类型获取对应的时间字符串
     *
     * @param type 枚举类型
     * @return 对应的时间字符串
     */
    public static String getDateStringByType(int type) {
        ExpireTimeEnum expireTimeEnum = BY_TYPE.get(type);
        if (expireTimeEnum != null) {
            return expireTimeEnum.getDateStringAfterDays();
        } else {
            throw new IllegalArgumentException("No enum constant with type: " + type);
        }
    }

    /**
     * 根据天数获取对应的时间字符串
     *
     * @param days 天数
     * @return 对应的时间字符串
     */
    public static String getDateStringByDays(int days) {
        for (ExpireTimeEnum e : values()) {
            if (e.getDays() == days) {
                return e.getDateStringAfterDays();
            }
        }
        throw new IllegalArgumentException("No enum constant with days: " + days);
    }

    /**
     * 获取当前时间增加指定天数后的Date对象
     *
     * @return 增加指定天数后的Date对象
     */
    public Date getDateAfterDays() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime future;
        if (this.days == -1) {
            // 永久有效，假设为100年后
            future = now.plusYears(100);
        } else {
            future = now.plus(this.days, ChronoUnit.DAYS);
        }
        return Date.from(future.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 根据类型获取对应的Date对象
     *
     * @param type 枚举类型
     * @return 对应的Date对象
     */
    public static Date getDateByType(int type) {
        ExpireTimeEnum expireTimeEnum = BY_TYPE.get(type);
        if (expireTimeEnum != null) {
            return expireTimeEnum.getDateAfterDays();
        } else {
            throw new IllegalArgumentException("No enum constant with type: " + type);
        }
    }

    /**
     * 根据天数获取对应的Date对象
     *
     * @param days 天数
     * @return 对应的Date对象
     */
    public static Date getDateByDays(int days) {
        for (ExpireTimeEnum e : values()) {
            if (e.getDays() == days) {
                return e.getDateAfterDays();
            }
        }
        throw new IllegalArgumentException("No enum constant with days: " + days);
    }

    /**
     * 获取当前时间的Date对象
     *
     * @return 当前时间的Date对象
     */
    public static Date getCurrentDate() {
        LocalDateTime now = LocalDateTime.now();
        return Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 获取当前时间的字符串表示
     *
     * @return 当前时间的字符串表示，格式为 "yyyy-MM-dd HH:mm:ss"
     */
    public static String getCurrentDateString() {
        LocalDateTime now = LocalDateTime.now();
        return DateFormatter.formatter.format(now);
    }
}